from concurrent.futures import ThreadPoolExecutor, as_completed, wait, FIRST_COMPLETED
from concurrent.futures import Future
# 未来对象，task的返回容器

import time


def get_html(times):
    time.sleep(times)
    print("getting page {} text success ".format(times))
    return times

executor = ThreadPoolExecutor(max_workers=3)

# task1 = executor.submit(get_html, (3))
# task2 = executor.submit(get_html, (2))

urls = [3, 2, 4]
all_task = [executor.submit(get_html, (url)) for url in urls]
wait(all_task, return_when=FIRST_COMPLETED)
print('main')
# for future in as_completed(all_task):
#     data = future.result()
#     print('get {} page success'.format(data))

# for data in executor.map(get_html, urls):
#     print('get {} page'.format(data))



# print(task1.done())
# print(task2.cancel())
# time.sleep(3)
# print(task1.done())
#
# print(task1.result())

